import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_vant/main.dart';

enum VanLoadingType { circular, spinner }

class VanLoading extends HookWidget {
  // 大小
  final double? size;
  // 内容
  final String? text;
  // 是否垂直排列
  final bool? vertical;
  // 加载类型
  final VanLoadingType? type;
  // 颜色
  final dynamic color;
  const VanLoading(
      {Key? key,
      this.size = 30,
      this.text = '',
      this.vertical = false,
      this.type = VanLoadingType.circular,
      this.color = 'white'})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    Map getStyle() {
      return {
        'main': {
          CssRule.marginRight: 5.0,
          CssRule.marginTop: 2.0,
          CssRule.alignItems: 'center',
          CssRule.justifyContent: 'center',
          CssRule.flexDirection: vertical == true ? 'col' : 'row',
          CssRule.color: 'white'
        },
        'content': {
          CssRule.marginLeft: 10,
          CssRule.fontSize: 15,
          CssRule.marginTop: vertical == true ? 10 : 0
        }
      };
    }

    renderLoadingContent() {
      if (type == VanLoadingType.circular) {
        return CircularProgressIndicator(
            strokeWidth: 1,
            valueColor: AlwaysStoppedAnimation(HexColor(color)));
      } else {
        return CupertinoActivityIndicator(
          color: HexColor(color),
        );
      }
    }

    renderLoading() {
      return SizedBox(width: size, height: size, child: renderLoadingContent());
    }

    renderContent() {
      if (text == null) return SizedBox();
      return TextView(
        text ?? '加载中...',
        styles: getStyle()['content'],
      );
    }

    return Frame(
      styles: getStyle()['main'],
      children: [renderLoading(), renderContent()],
    );
  }
}
